package com.vrv.app.cyberspace.common.ddio;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.util.Asserts;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DDIO 配置和方法封装
 */
@Component
@ConfigurationProperties(ApiV2Service.PREFIX)
public class ApiV2Service {

    public static final String PREFIX = "ddio";

    private String appID = "4328613580";
    private String appSecret = "fmFH8hlJa6rmEMSMHhVmig";
    private String serverUrl = "http://vrv.linkdood.cn";

    private AccessToken accessToken = new AccessToken();

    private String getAccessToken() {
        Map<String, String> params = new HashMap<>();
        params.put("accessType", "CLIENT");
        params.put("appID", appID);
        params.put("appSecret", appSecret);
        Response res = HttpUtil.get(getBaseUrl() + "token", params);
        JSONObject rj = JSON.parseObject(res.getBody());
        return rj.getJSONObject("result").getString("access_token");
    }

    private String accessToken() {
        Asserts.notBlank(appID, "appID can't blank");
        Asserts.notBlank(appSecret, "appSecret can't blank");
        Asserts.notBlank(getBaseUrl(), "baseUrl can't blank");
        if (!accessToken.enable()) {
            String tokenStr = getAccessToken();
            accessToken.setTokenStr(tokenStr);
            accessToken.setExpAt(System.currentTimeMillis() + 7000 * 1000l);
        }
        return accessToken.getTokenStr();
    }

    /**
     * 发送模板消息
     */
    public String sendTemplateMsg(TemplateMessage tm) {
        String url = getBaseUrl() + "message/sendMessageTemplate";
        Map<String, String> headers = new HashMap<>();
        headers.put("access-token", accessToken());
        Response res = HttpUtil.postJson(url, tm, headers);
        return res.getBody();
    }

    /**
     * 获取所有用户
     */
    public String getAllUser(Integer pageNum, Integer pageSize) {
        String url = getBaseUrl() + "org/getAllUser";
        Map<String, String> headers = new HashMap<>();
        headers.put("access-token", accessToken());
        Map<String, String> params = new HashMap<>();
        params.put("pageNum", pageNum + "");
        params.put("pageSize", pageSize + "");
        Response res = HttpUtil.post(url, params, headers);
        return res.getBody();
    }

    /**
     * 获取所有组织
     */
    public String getAllOrg() {
        String url = getBaseUrl() + "org/getAllOrg";
        Map<String, String> headers = new HashMap<>();
        headers.put("access-token", accessToken());
        Response res = HttpUtil.get(url, null, headers);
        return res.getBody();
    }

    /**
     * 获取组织机构
     */
    public String getOrg(String orgID) {
        String url = getBaseUrl() + "org/getOrg?orgID=" + orgID;
        Map<String, String> headers = new HashMap<>();
        headers.put("access-token", accessToken());
        Response res = HttpUtil.get(url, null, headers);
        return res.getBody();
    }

    /**
     * 批量获取用户所在组织机构
     */
    public String getOrgList(List<String> linkdoods) {
        String url = getBaseUrl() + "org/getOrgList" ;
        Map<String, String> headers = new HashMap<>();
        headers.put("access-token", accessToken());
        Response res = HttpUtil.postJson(url, linkdoods, headers);
        return res.getBody();
    }

    /*********** getter&setter ***********/
    public String getAppID() {
        return appID;
    }

    public void setAppID(String appID) {
        this.appID = appID;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getBaseUrl() {
        return serverUrl + "/platform/api2/";
    }
}
